<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\Db;
use think\facade\View;
use app\common\model\Area as AreaModel;
use think\response\Json;
use app\common\model\User;

class Area extends Base
{
    protected $model;

    protected $pk = 'id';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new AreaModel();
    }

    public function index(): string
    {
        View::assign('regions', $this->model->getRegions());
        return View::fetch();
    }

    public function edit($name = '', $id = '', $pid = '', $uid = '')
    {
        try {
            $p_name = '';
            $info = $this->model->where('code', $id)->find();
            $user_code = $pasture_code = $contract_code = '';
            if ($info->uid) {
                $user_code = User::where('id', $info->uid)->value('code');
            }
            if ($info->pasture_uid) {
                $pasture_code = User::where('id', $info->pasture_uid)->value('code');
            }
            if ($info->contract_uid) {
                $contract_code = User::where('id', $info->contract_uid)->value('code');
            }
            View::assign('p_name', $p_name);
            View::assign('info', $info);
            View::assign('user_code', $user_code);
            View::assign('pasture_code', $pasture_code);
            View::assign('contract_code', $contract_code);
        } catch (\Exception $e) {

            return $e->getMessage();
        }

        return View::fetch();
    }

    public function save1(): Json
    {
        $param = $this->request->post();
        Db::startTrans();
        try{
            $area = AreaModel::where('code', $param['id'])->find();
            $old_uid = $area->uid;
            $old_contract_uid = $area->contract_uid;
            $uid = $contract_uid = 0;
            if (!empty($param['user_code'])) {
                if(!$uid = User::where('code', $param['user_code'])->value('id')){
                    return json(['code' => -1, 'msg' => $param['user_code'].'会员不存在']);
                }
            }

            if (!empty($param['contract_code'])) {
                if(!$contract_uid = User::where('code', $param['contract_code'])->value('id')){
                    return json(['code' => -1, 'msg' => $param['contract_code'].'会员不存在']);
                }
            }

            //更新会员表
            if ($old_uid != $uid) {
                AreaModel::agentLog($uid, $area, $this->admin, 'uid');
                $area->uid = $uid;
                $area->save();

                AreaModel::agentLevel($old_uid);
                AreaModel::agentLevel($uid);
            }

            if ($old_contract_uid != $contract_uid) {
                AreaModel::agentLog($contract_uid, $area, $this->admin, 'contract_uid');
                $area->contract_uid = $contract_uid;
                $area->save();

                AreaModel::agentLevel($old_contract_uid, 'contract_uid', 'contract_agent_level');
                AreaModel::agentLevel($contract_uid, 'contract_uid', 'contract_agent_level');
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

        return json(['code' => 1, 'msg' => '操作成功']);

    }

    public function update($key, $val, $id): Json
    {
        try {
            if ($val == 'true') $val = 1;
            if ($val == 'false') $val = 0;
            $this->model::update([$key => $val], ['id' => $id]);
            return json(['code' => 1, 'msg' => '操作成功']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
}
